<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <button @click="showStore">显示仓库内容</button>
        <h1>{{this.$store.state.counter}}</h1>
        <button @click="add">加1</button>
    </div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<!-- 1 引入vuex依赖 -->
<script src="./node_modules/vuex/dist/vuex.js"></script>
<script>
    //2创建仓库
    let INCREMENT = "increment"
    let store = new Vuex.Store({
        //仓库中的数据
        state: {
            //改变值得操作
            counter: 888,
        },
        mutations: {
            [INCREMENT]: function(state, {
                payload
            }) {
                state.counter += payload.cunt;
            }
        }
    })
    let vm = new Vue({
        //3配置仓库
        store,
        el: "#app",
        methods: {
            showStore: function() {
                console.log(this.$store);
            },
            add: function() {
                // this.$store.state.counter += 1;直接修改不推荐
                //提交commit修改
                // this.$store.commit("increment", 10);
                this.$store.commit({
                    type: "increment",
                    payload: {
                        cunt: 10
                    }
                })
            }
        },
    })
</script>

</html>